package com.ruoyi.article.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.article.domain.Material;
import com.ruoyi.article.dtos.Result;
import com.ruoyi.article.enums.MessageConstant;
import com.ruoyi.article.mapper.MaterialMapper;
import com.ruoyi.article.service.MaterialService;


import com.ruoyi.config.AliOssUtil;
import com.ruoyi.config.SnowflakeIdGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.UUID;


@Service
@Slf4j
public class MaterialServiceImpl implements MaterialService {


    @Autowired
    private AliOssUtil aliOssUtil;

    @Resource
    private MaterialMapper materialMapper;

    @Resource
    private SnowflakeIdGenerator idGenerator;

    @Override
    public Result uploadPicture(MultipartFile file) {


        log.info("文件上传：{}", file);
        try {
            //原始文件名
            /*首先通过file.getOriginalFilename()获取原始文件名*/
            String originalFilename = file.getOriginalFilename();
            //截取原始文件名的后缀   dfdfdf.png
            /*然后通过originalFilename.lastIndexOf(".")获取文件名的后缀。*/
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            //构造新文件名称
            /*使用UUID.randomUUID().toString()生成一个随机的文件名，并将后缀拼接在文件名后面，构造出新的文件名。*/
            String objectName = "material/" + UUID.randomUUID().toString() + extension;

            //文件的请求路径
            /*然后，调用aliOssUtil.upload方法将文件上传到OSS，并获取文件的请求路径。*/
            String filePath = aliOssUtil.upload(file.getBytes(), objectName);

            Long id = idGenerator.nextId();//雪花id

            materialMapper.addMaterialPhoto(id, filePath);


            /*最后，返回一个Result对象，其中包含上传文件的请求路径。*/
            return Result.success(filePath);
        } catch (IOException e) {
            log.error("文件上传失败：{}", e);
        }

        return Result.error(MessageConstant.UPLOAD_FAILED);


    }


    @Override
    public Result uploadPictureStatus(long id,Integer isSubmit) {

        isSubmit=isSubmit==1?0:1;

        int status = materialMapper.uploadPictureStatus(id,isSubmit);

        if (status > 0) {
            return Result.success();
        }
      return   Result.error("素材状态更改错误");

    }

    @Override
    public Result deletePicture(Long id) {


     int result=   materialMapper.deletePicture(id);

     if(result>0){
         return Result.success();
     }

        return Result.error("删除素材失败");
    }

    @Override
    public PageInfo<Material> listMaterials(int pageNum, int pageSize) {

        PageHelper.startPage(pageNum, pageSize);
        // 2. 执行 MyBatis 查询（会自动拼接分页 SQL）
        List<Material> list = materialMapper.listMaterials();
        // 3. 封装分页信息（总条数、总页数等）
        return new PageInfo<>(list);
    }


    @Override
    public Result getPhotoUrl() {
     List<String> list=  materialMapper.getPhotoUrl(1);

     if(list!=null || list.size()!=0){
         return Result.success(list);
     }
        return Result.error("请求图片失败");
    }
}
